﻿using Kaskelot.SP.Core;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;
using Stockholm.SOA.Kaskelot.WCF.FaultContracts;
using Stockholm.SOA.Kaskelot.WCF.ServiceContracts;
using System;
using System.Collections.Generic;
using System.ServiceModel;

namespace Kaskelot.SP.Services
{
	public class KursService : BaseService<IKaskelotService>
	{
		#region Kurser
		public IEnumerable<Kurs> GetKurser()
		{
			IEnumerable<Kurs> result = null;
			try
			{
				result = this.channel.GetKurser(ApplicationContext.Current.Val.Id);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetKurser", "Error getting kurser", ex);
				throw;
			}
			return result;
		}

		public Kurs GetKurs(Guid kursId)
		{
			Kurs result = null;
			try
			{
				result = this.channel.GetKurs(ApplicationContext.Current.Val.Id, kursId);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetKurs", "Error getting kurs", ex);
				throw;
			}
			return result;
		}

		public void UpdateKurs(Kurs k)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.UpdateKurs(k);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "UpdateKurs", "Error updating kurs", ex);
				throw;
			}
		}

		public void CreateKurs(Kurs k)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.CreateKurs(k);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "CreateKurs", "Error creating kurs", ex);
				throw;
			}
		}

		public void DeleteKurs(Guid id)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.DeleteKurs(ApplicationContext.Current.Val.Id, id);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "DeleteKurs", "Error deleting kurs", ex);
				throw;
			}
		}

		#endregion

		#region KursTillfälle
		public NotifieringsResultat SkickaSMS(IEnumerable<string> mottagare, string text)
		{
			NotifieringsResultat result = null;
			try
			{
				result = this.channel.SkickaSMS(mottagare, text);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "SkickaSMS", "Error sending SMS", ex);
				throw;
			}
			return result;
		}

		public IEnumerable<KursTillfalle> GetKursTillfallenByKursId(Guid? kursId)
		{
			IEnumerable<KursTillfalle> result = null;
			try
			{
				result = this.channel.GetKursTillfallenByKursId(ApplicationContext.Current.Val.Id, kursId);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetKursTillfalleByKursId", "Error getting kurstillfallen", ex);
				throw;
			}
			return result;
		}

		public KursTillfalle GetKursTillfalle(Guid kursId)
		{
			KursTillfalle result = null;
			try
			{
				result = this.channel.GetKurstillfalle(ApplicationContext.Current.Val.Id, kursId);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetKursTillfalle", "Error getting kurstillfalle", ex);
				throw;
			}
			return result;
		}

		public void UpdateKursTillfalle(KursTillfalle kt)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.UpdateKurstillfalle(kt);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "UpdateKursTillfalle", "Error updating kurstillfalle", ex);
				throw;
			}
		}

		public void CreateKursTillfalle(KursTillfalle kt)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.CreateKurstillfalle(kt);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "CreateKursTillfalle", "Error creating kurstillfalle", ex);
				throw;
			}
		}

		public void DeleteKursTillfalle(Guid id)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.DeleteKurstillfalle(ApplicationContext.Current.Val.Id, id);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "DeleteKursTillfalle", "Error deleting kurstillfalle", ex);
				throw;
			}
		}

		public IEnumerable<KursTillfalle> GetKursTillfalleByPersonId(Guid id)
		{
			IEnumerable<KursTillfalle> result = null;
			try
			{
				result = this.channel.GetKursTillfalleByPersonId(ApplicationContext.Current.Val.Id, id);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetKursTillfalleByPersonId", "Error getting kurstillfalle", ex);
				throw;
			}
			return result;
		}
		#endregion

		#region PersonKursTillfälle
		public void AddPersonKursTillfalle(Guid kurstillfalleId, int antal, string skapadAv)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.AddPersonKursTillfalle(ApplicationContext.Current.Val.Id, kurstillfalleId, antal, skapadAv);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "AddPersonKursTillfalle", "Error adding personkurstillfalle", ex);
				throw;
			}
		}

		public void DeletePersonKursTillfalle(Guid id)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.DeletePersonKursTillfalle(ApplicationContext.Current.Val.Id, id);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "DeletePersonKursTillfalle", "Error deleting personkurstillfalle", ex);
				throw;
			}
		}

		public IEnumerable<PersonKursTillfalle> GetPersonKursTillfallenByKursTillfalleId(Guid kurstillfalleId)
		{
			IEnumerable<PersonKursTillfalle> result = null;
			try
			{
				result = this.channel.GetPersonKursTillfallenByKursTillfalleId(ApplicationContext.Current.Val.Id, kurstillfalleId);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetPersonKursTillfalleByKursTillfalleId", "Error getting personkurstillfallen", ex);
				throw;
			}
			return result;
		}

		public void UpdatePersonKursTillfalle(PersonKursTillfalle pkt)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.UpdatePersonKursTillfalle(pkt);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "UpdatePersonKursTillfalle", "Error updating personkurstillfalle", ex);
				throw;
			}
		}

		public PersonKursTillfalle GetPersonKursTillfalle(Guid id)
		{
			PersonKursTillfalle result = null;
			try
			{
				result = this.channel.GetPersonKursTillfalle(ApplicationContext.Current.Val.Id, id);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetPersonKursTillfalle", "Error getting personkurstillfalle", ex);
				throw;
			}
			return result;
		}

		public void PlaceraValdistriktTillKursTillfalle(Guid valdistriktId, Guid kurstillfalleId)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.PlaceraValdistriktTillKursTillfalle(ApplicationContext.Current.Val.Id, valdistriktId, kurstillfalleId);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "PlaceraValdistriktTillKursTillfalle", "Error adding valdistrikt to kurstillfalle", ex);
				throw;
			}
		}

		public void PlaceraGruppTillKursTillfalle(Guid gruppId, Guid kurstillfalleId)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.PlaceraGruppTillKursTillfalle(ApplicationContext.Current.Val.Id, gruppId, kurstillfalleId);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "PlaceraGruppTillKursTillfalle", "Error adding grupp to kurstillfalle", ex);
				throw;
			}
		}
		#endregion
	}
}
